// 留学贴士模型
const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');
const User = require('./User');

const Tip = sequelize.define('Tip', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: '贴士标题'
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: '贴士内容'
  },
  type: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: '贴士类型'
  },
  coverImage: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: '封面图片'
  },
  authorId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: User,
      key: 'id'
    },
    comment: '作者ID'
  },
  viewCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '浏览次数'
  },
  likeCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '点赞次数'
  },
  commentCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '评论次数'
  },
  status: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '状态 1:正常 2:审核中 3:已下架 4:已禁用'
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  },
  updatedAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  }
}, {
  tableName: 'tips',
  timestamps: true,
  charset: 'utf8mb4',
  collate: 'utf8mb4_unicode_ci'
});

// 关联关系
Tip.belongsTo(User, { as: 'author', foreignKey: 'authorId' });

module.exports = Tip;